package com.ak.renderer;

import java.util.ArrayList;
import java.util.List;

import com.ak.cluster.GameObject;
import com.ak.cluster.GameWorld;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;



public class WorldRenderer {

	static float residualTime = 0.0f;
	final float WORLD_STEP = 1.0f / 60.0f;
	public Box2DDebugRenderer debugRenderer;
	public GameWorld rWorld;
	public WorldRenderer(GameWorld lWorld)
	{
		this.rWorld = lWorld;
		debugRenderer = new Box2DDebugRenderer();
	}

	public void render()
	{
        residualTime += Gdx.graphics.getDeltaTime();
        while( residualTime >= WORLD_STEP )

        {
                rWorld.pWorld.step(WORLD_STEP, 3, 3);
                residualTime -= WORLD_STEP;

        }
		rWorld.worldCamera.update();
		rWorld.worldCamera.apply(Gdx.gl10);
		for(GameObject uO : this.rWorld.getObjects())
		{
			//static
			if(uO.chosenRenderSetting == 0)
			{
				uO.assetMesh.bind();
				uO.assetTexture.bind();
				Gdx.gl10.glPushMatrix();
				Gdx.gl10.glTranslatef(uO.position.x, uO.position.y, 0.0f);
				Gdx.gl10.glScalef(uO.scale.x,uO.scale.y, 0.0f);
				uO.assetMesh.render(GL10.GL_TRIANGLE_FAN);
				Gdx.gl10.glPopMatrix();
				uO.assetMesh.unbind();
			}
			//dynamic
			if(uO.chosenRenderSetting == 1)
			{
				uO.assetMesh.bind();
				Gdx.gl10.glEnable(GL10.GL_BLEND);
				Gdx.gl10.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
				uO.assetTexture.bind();
				//Gdx.gl10.glRotatef(uO.objectBody.getAngle(), uO.position.x, uO.position.y, 0.0f); // opengl rotate not currently working
				Gdx.gl10.glPushMatrix();
				Gdx.gl10.glTranslatef(uO.position.x,uO.position.y,0.0f);
				Gdx.gl10.glScalef(uO.scale.x,uO.scale.y, 0.0f);
				Gdx.gl10.glRotatef((float)Math.toDegrees(uO.objectBody.getAngle()), 0.0f, 0.0f, 1.0f);
				uO.assetMesh.render(GL10.GL_TRIANGLE_FAN);
				Gdx.gl10.glPopMatrix();
				Gdx.gl10.glDisable(GL10.GL_BLEND);
				uO.assetMesh.unbind();
			}
		}
		debugRenderer.render(rWorld.pWorld, rWorld.worldCamera.combined);
	}
	public void dispose()
	{
		debugRenderer = null;
	}
}
